#!/bin/bash -xe
# written by huibo.zhao

FLAG_DEPS=N
FLAG_NODE=N
FLAG_NVIM=N
FLAG_INIT=N

DIR_CURR=$(cd $(dirname "$0"); pwd)  && cd ${DIR_CURR}
[ ${UID} -ne 0 ] && { echo -e "\033[1;31m Must run the script as root.\033[0m"; exit 1; }

function inst_deps() {
apt install -y vim curl wget git unzip links universal-ctags cscope global
grep 'alias vi='  ~/.bashrc  || sed -i "/^alias ll=/a alias vi='vim'"      ~/.bashrc
grep 'alias dir=' ~/.bashrc  || sed -i "/^alias ll=/a alias dir='ls -lsh'" ~/.bashrc
wget -P ~/.config/nvim/autoload/ https://raw.gitcode.com/gh_mirrors/vi/vim-plug/blobs/5c910cab422f13794b5e78fd366c8e3121c23348/plug.vim
sed -i '/^FLAG_DEPS/ s/=.*/=Y/' ${0}  && sleep 1
}

function inst_node() {
wget -c https://github.com/Schniz/fnm/releases/latest/download/fnm-linux.zip && \
sudo unzip fnm-linux.zip -d /usr/local/bin/ && \
sudo chmod +x /usr/local/bin/fnm && \
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.bashrc && \
source  ~/.bashrc && \
fnm list-remote && \
fnm use --install-if-missing 22 --node-dist-mirror https://npmmirror.com/mirrors/node && \
node -v && npm -v
sed -i '/^FLAG_NODE/ s/=.*/=Y/' ${0}  && sleep 1
}

function inst_nvim() {
wget -P /usr/bin/ https://github.com/neovim/neovim/releases/download/stable/nvim-linux-x86_64.appimage && \
chmod +x /usr/bin/nvim-linux-x86_64.appimage && \
ln -Tsv  /usr/bin/nvim-linux-x86_64.appimage  /usr/bin/nvim && \
ln -Tsv  /usr/bin/nvim           /usr/bin/nv && \
touch  ~/.config/nvim/init.vim
sed -i '/^FLAG_NVIM/ s/=.*/=Y/' ${0}  && sleep 1
}

function conf_init() {
cat > ~/.config/nvim/init.vim << 'EOF'
"Settings by hbzhao
colorscheme elflord
filetype plugin indent on
syntax on
set number
set softtabstop=4
set shiftwidth=4
set laststatus=2
set statusline=%m[%F]%y[%{&ff}]%*%=[%l:%c][%p%%]
set fileencodings=utf-8,gbk,cp936,ucs-bom
set fileencoding=utf-8
set encoding=utf-8
set termencoding=utf-8
set nocompatible
set nobackup
set noswapfile

map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h
inoremap ;; <ESC>
nnoremap QQ :qa!<CR>

set completeopt=longest,menu,menuone  
set nocompatible
syntax enable
filetype plugin indent on

set tags=tags;/
set autochdir
set tags+=~/.vim/systags
"cs add cscope.out
"set cscopequickfix=s-,c-,d-,i-,t-,e-

map <F2> :NERDTreeToggle<CR>
map <F3> :AsyncRun<Space>
map <silent> <F4> :TagbarToggle<CR>
map <F8> :!ctags -R --c++-kinds=+p --fields=+iaS --extras=+q .<CR>
map <F9> :call asyncrun#quickfix_toggle(8)<cr>
let g:asyncrun_open = 8
let g:asyncrun_encs = 'gbk'

let g:tagbar_ctags_bin='ctags'
let g:tagbar_width=30
let g:tagbar_left = 1
let g:tagbar_autofocus=1

let g:indent_guides_guide_size = 1
let g:indent_guides_start_level = 2

call plug#begin('~/.config/nvim/plugged')
Plug 'scrooloose/nerdtree'
Plug 'skywind3000/asyncrun.vim'
Plug 'preservim/tagbar'
Plug 'vim-scripts/AutoComplPop'
Plug 'vim-scripts/OmniCppComplete'
Plug 'Yggdroot/indentLine'
"Plug 'jsfaint/gen_tags.vim'
Plug 'neoclide/coc.nvim',{'branch':'release'}
call plug#end()
EOF
sed -i '/^FLAG_INIT/ s/=.*/=Y/' ${0}  && sleep 1
}

[ x${FLAG_DEPS} = xN ] && inst_deps
[ x${FLAG_NODE} = xN ] && inst_node
[ x${FLAG_NVIM} = xN ] && inst_nvim
[ x${FLAG_INIT} = xN ] && conf_init